package com.khai.app2;

import com.khai.sdk.dto.ReqVo;
import com.khai.sdk.dto.ResVo;
import com.khai.sdk.dto.UserReqVo;
import com.khai.sdk.dto.UserResVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Api2Service {

    @RequestMapping(value = "findUser")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "findUserFail",
            threadPoolProperties = {
                    @HystrixProperty(name = "coreSize", value = "1"),
                    @HystrixProperty(name = "maxQueueSize", value = "2")
            }
    )
    public ResVo<UserResVo> findUser(ReqVo<UserReqVo> vo) {
        System.err.println("线程名称 findUser:" + Thread.currentThread().getName());
        UserResVo userResVo = new UserResVo();
        userResVo.setId(System.currentTimeMillis() + "A");
        ResVo<UserResVo> resVo = new ResVo<>();
        resVo.setResBody(userResVo);
        return resVo;
    }

    public ResVo<UserResVo> findUserFail(ReqVo<UserReqVo> vo) {
        System.err.println("线程名称 findUserFail :" + Thread.currentThread().getName());
        UserResVo userResVo = new UserResVo();
        userResVo.setId(System.currentTimeMillis() + "降级/熔断");
        ResVo<UserResVo> resVo = new ResVo<>();
        resVo.setResBody(userResVo);
        return resVo;
    }

    @RequestMapping(value = "/person2")
    @ResponseBody
    public Object person2() {
        System.err.println("线程名称findPerson:" + Thread.currentThread().getName());
        return "正常访问:线程隔离";
    }

}
